/*
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.dspl.validate.violation;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.dspl.model.DataType;
import com.google.dspl.model.Value;
import com.google.dspl.validate.ItemReference;

import java.util.List;

/**
 * Violation for a table column which has a constant value, and that constant value does not match
 * the column's type.
 *
 * @author Maryla Ustarroz Calonge
 */
public final class ColumnConflictingConstantValueTypeViolation extends Violation {

  private static final long serialVersionUID = -8740388509168476958L;

  private final String columnId;
  private final String tableId;
  private final Value constantValue;
  private final DataType columnType;

  public ColumnConflictingConstantValueTypeViolation(
      ItemReference location,
      String columnId,
      String tableId,
      Value constantValue,
      DataType columnType) {
    super(Severity.ERROR, location);
    this.columnId = columnId;
    this.tableId = tableId;
    this.constantValue = Preconditions.checkNotNull(constantValue);
    this.columnType = Preconditions.checkNotNull(columnType);
  }

  @Override
  public String getDefaultTemplate() {
    return "Column %s on table %s has a constant value %s that is not a valid value for the type "
    	+ "of the column %s";
  }

  @Override
  public List<Object> getDefaultTemplateParameters() {
    return ImmutableList.<Object>of(columnId, tableId, constantValue, columnType);
  }

  public String getColumnId() {
    return columnId;
  }

  public String getTableId() {
    return tableId;
  }

  public Value getConstantValue() {
    return constantValue;
  }

  public DataType getColumnType() {
    return columnType;
  }

  @Override
  public <T> T accept(ViolationVisitor<T> visitor) {
    return visitor.visit(this);
  }
}
